#include<bits/stdc++.h>
using namespace std;

struct node {
	int value;
	int right, left;
};
vector<node> tree;
vector<int> num, ans;
int n;

void dfs(int root, int& i) {
	if (root == -1)
		return;
	dfs(tree[root].left, i);
	tree[root].value = num[i++];
	dfs(tree[root].right, i);
}

void bfs(int root) {
	queue<int> que;
	que.push(root);
	while (!que.empty()) {
		int now = que.front();
		que.pop();
		ans.push_back(tree[now].value);
		if (tree[now].left != -1)
			que.push(tree[now].left);
		if (tree[now].right != -1)
			que.push(tree[now].right);
	}
}

int main() {
	scanf("%d", &n);
	tree.resize(n);
	num.resize(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d%d", &tree[i].left, &tree[i].right);
	}
	for (int i = 0; i < n; ++i) {
		scanf("%d", &num[i]);
	}
	sort(num.begin(), num.end());
	int root = 0;
	dfs(0, root);

	bfs(0);

	for (int i = 0; i < n; ++i) {
		if (i)
			printf(" ");
		printf("%d", ans[i]);
	}
	return 0;
}